How to Avoid Duplicate Post Display with Multiple Loops in WordPress | 您所在的位置:网站首页 › wordpress duplicate post or page function github › How to Avoid Duplicate Post Display with Multiple Loops in WordPress |
Last updated on September 23rd, 2021 by Editorial Staff | Reader DisclosureDisclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Shares 33 Share Tweet Share Facebook Messenger WhatsApp EmailAre you trying to avoid duplicate post appearing with multiple loops in WordPress? If you are running multiple WordPress queries to display different sets of posts, then you may come across this issue. Some posts may match more than one loop and would appear twice. In this article, we will show you how to easily avoid duplicate post display with multiple loops in WordPress. How Duplicate Posts Appear in Multiple WordPress LoopsYou can use multiple WordPress loops in a template when creating a custom WordPress theme or a custom page template. For instance, some users may want to display their recent posts along with their most popular posts. Some users may want to display recent posts followed by posts in different categories. Now let’s suppose that a post you recently published also matches the criteria for multiple loops. WordPress will display it again unless you exclude it. Now since you are dynamically generating posts for each loop, you cannot manually predict if a duplicate post would appear in a loop. That being said, let’s take a look at how to easily avoid duplicate posts when dealing with multiple loops in WordPress. Avoiding Duplicate Posts in Multiple WordPress LoopsFor this tutorial, we will show you some sample WordPress code. You can then modify it to your own requirements. First, let’s recreate the duplicate posts issue. In the following sample code, we are displaying posts for two categories without avoiding duplicate posts. As you can see, this code does not check for duplicate posts in either query. It will go on and display duplicate posts if they are found in both categories: Let’s fix this issue. In order to avoid duplicate posts, we need to temporarily store the data about the posts displayed in the first loop. Once we have that information, we’ll simply modify our second query to exclude those posts from reappearing. Here is a sample code that avoids duplicate posts from appearing in the second loop. In the above code, we stored Post IDs in an array $exclude. After that, we added post__not_in argument to our second query to exclude posts that were displayed in the first loop. That’s all you can now visit your website to see the duplicate posts disappear from the second loop. We hope this article helped you learn how to avoid duplicate post display with multiple loops in WordPress. You may also want to see our comprehensive WordPress theme development cheat sheet for more tips. If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook. Shares 33 Share Tweet Share Facebook Messenger WhatsApp Email Popular on WPBeginner Right Now!12+ Things You MUST DO Before Changing WordPress Themes Revealed: Why Building an Email List is so Important Today (6 Reasons) How to Start a Podcast (and Make it Successful) in 2023 How to Properly Move Your Blog from WordPress.com to WordPress.org Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. About the Editorial StaffEditorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience building WordPress websites. We have been creating WordPress tutorials since 2009, and WPBeginner has become the largest free WordPress resource site in the industry. The Ultimate WordPress Toolkit Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have! Download NowReader Interactions15 CommentsLeave a ReplyNick J says Oct 23, 2021 at 5:04 pm Is there a plugin that does this? ReplySatriyo says Mar 1, 2018 at 11:17 am Could someone tell me how to do this? I’m really new to this and need a help, please give me a clear example with the post ID, how to store it? Let’s say, mine is 1527. ReplyGaurav says Mar 1, 2018 at 7:43 am I’m running 2 loops before loops of a specific category in which I would like to avoid duplicates. So how do a store ID’s in the array from first two loops? ReplyJoe says Oct 11, 2017 at 9:01 am Just what I was looking for – thank you! ReplyGuilherme Alves says Apr 9, 2017 at 10:00 am Thank you soo much :))) This helps me alot! Save my day Julie says Feb 25, 2016 at 5:24 am AWESOME!! Thank you so much! And thank you SERGEYVLASOV for that last comment– Worked like a charm for my multiple loops. Hooray!! ReplyPirooz says Feb 3, 2016 at 11:07 am This method just works fine until both of 2 loops located in one file. but when I put the first loop in the header.php and another one in the index.php, in_array($post->ID, $do_nit_duplicate) returns null. what can I do? Replywarren says Dec 7, 2015 at 8:38 am Good afternoon all, will this work for my current issue with double display of posts on site? it literally displays a copy under the posts and the 1, 2, -> button… the site is I have deactivated re-activated plugins i am literally going nuts. Replyagus says Oct 21, 2013 at 1:20 am can you help me? I have proble with duplicate category in my site please #thanks ReplyWPBeginner Support says Oct 21, 2013 at 12:16 pm Seems like a theme specific issue, contact theme developers for support. ReplyAdmin Gabriel says Dec 27, 2012 at 12:39 pm Before iterating over the default loop shouldn’t we use wp_reset_postdata(); ? ReplyGreg says Dec 11, 2012 at 1:45 pm @sergeyvlasov – Thanks that worked for me tho i changed $do_nit_duplicate to $do_not_duplicate Reply Ron Hantman says Aug 15, 2012 at 2:04 pm Checkout this solution which does this task outside of the loop: http://wordpress.stackexchange.com/questions/61936/removing-duplicate-values-between-two-wordpress-queries/ Replysergeyvlasov says Mar 30, 2012 at 5:23 pm I think there is a flaw in this algorithm. It can spot no more than 1(one) duplication. So the magic line would look like $do_not_duplicate[] = $post->ID and then used as if(in_array($post->ID, $do_nit_duplicate)) continue; Reply Leave A Reply Cancel reply | How to Avoid Duplicate Post Display with Multiple Loops in WordPress
CopyRight 2018-2019 实验室设备网 版权所有 |